package com.ychs.lawyer.service.impl;
import com.ychs.lawyer.DTO.RoleMenu;
import com.ychs.lawyer.bean.Menu;
import com.ychs.lawyer.bean.Role;
import com.ychs.lawyer.mapper.MenuMapper;
import com.ychs.lawyer.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: clh
 * @Date: 2024/08/05/15:41
 * @Description:
 */

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    /**
     * 根据角色id获取该角色所拥有的菜单
     * @param roleId
     * @return
     */
    @Override
    public List<Menu> getMenusByRoleId(String roleId) {
        List<Menu> menus = menuMapper.getMenusByRoleId(roleId);
        List<Menu> list = menus.stream()
                .filter((menu) -> {
                    return menu.getPid() == 0;
                })
                .sorted(Comparator.comparing(Menu::getNum))
                .map((menu) -> {
//                    参数：每一个0级的菜单 ，所有roleid对应出来的菜单
                    menu.setSubs(getSub(menu, menus));
                    return menu;
                })
                .collect(Collectors.toList());
        return list;
    }


    private List<Menu> getSub(Menu menu, List<Menu> menus) {
        return menus.stream()
                .filter(m -> {
                    return m.getPid() == menu.getMenuId();
                })
                .sorted(Comparator.comparing(Menu::getNum))
                .map(m -> {
//                    //递归
                    m.setSubs(getSub(m,menus));
                    return m;
                })
                .collect(Collectors.toList());
    }




    @Override
    public List<Menu> getMenuTree() {
 // 获取菜单列表
    List<Menu> menus = menuMapper.getMenu();
        List<Menu> list = menus.stream()
                .filter((menu) -> {
                    return menu.getPid() == 0;
                })
                .sorted(Comparator.comparing(Menu::getNum))
                .map((menu) -> {
//                    参数：每一个0级的菜单 ，所有roleid对应出来的菜单
                    menu.setSubs(getSub(menu, menus));
                    return menu;
                })
                .collect(Collectors.toList());
        return list;
}
    @Override
    public int editMenu(Menu menu) {
         int res = menuMapper.editMenu(menu);
         return res;
    }
    @Override
    public int addMenu(Menu menu) {
        if(menu.getPid()==null) {
            int res = menuMapper.addMenu(menu);
            return res;
        }else {
            int res2 =  menuMapper.addMenu2(menu);
            return res2;
        }
    }

    @Override
    public List<Integer> getMenuByRoleId(RoleMenu roleMenu) {
        return menuMapper.getMenuByRoleId(roleMenu);
    }

    @Override
    public int delMenu(Integer menuId) {
        int i = menuMapper.selectRoleId(menuId);
        if (i > 0) {
            return 0;
        } else if (i == 0) {
            int i1 = menuMapper.delMenu(menuId);
            return i1;
        }
            return -1;
    }
}

